package com.example.exam.controller;

import com.example.exam.entity.*;
import com.example.exam.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;

/**
 * (Fillbacks)表控制层
 *
 * @author makejava
 * @since 2021-09-04 15:02:44
 */
@RestController
@RequestMapping("/fillbacks")
public class FillbacksController {
    /**
     * 服务对象
     */
    @Autowired
    private FillbacksService fillbacksService;
    @Autowired
    private TeacherService teacherService;
    @Autowired
    private CollegeService collegeService;
    @Autowired
    private MajorService majorService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private PaperService paperService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne/{id}")
    public Fillbacks selectOne(@PathVariable("id") Integer id) {
        return this.fillbacksService.queryById(id);
    }

    @RequestMapping("/selectAll")
    public List<Fillbacks> selectAll() {
        return this.fillbacksService.queryAll();
    }

    /**
     * 通过传入记录开始数，对其进行一页10个展示。
     *
     * @param page 记录开始数
     * @return 那一页的10条数据
     */
    @RequestMapping("/selectByPaper")
    public List<Fillbacks> selectByPaper(@RequestParam("page") Integer page, @RequestParam("limit") Integer limit, String question, String subject) {
        return this.fillbacksService.queryAllByLimit(page, limit, question, subject);
    }

    @RequestMapping("/deleteOne/{id}")
    public boolean deleteOne(@PathVariable("id") Integer id) {
        return this.fillbacksService.deleteById(id);
    }

    @RequestMapping("/insertOne")
    public Fillbacks insertOne(Fillbacks fillbacks) {
        return fillbacksService.insert(fillbacks);
    }

    @RequestMapping("/update")
    public Fillbacks update(Integer id, Fillbacks fillbacksVal,
                            HttpSession session) {
        Fillbacks fillbacks = fillbacksService.queryById(id);
        fillbacks.setAnswer(fillbacksVal.getAnswer());
        fillbacks.setCheck(fillbacksVal.getCheck());
        fillbacks.setQuestion(fillbacksVal.getQuestion());
        /*
        查找老师对应专业
         */
        int userId = (int) session.getAttribute("userId");
        boolean flag = false;
        String subject = fillbacksVal.getSubject();
        if (userId < 4000 && userId > 3000) {
            Teacher teacher = teacherService.queryById(userId);
            if (teacher != null) {
                String teaSubject = teacher.getInstitute();
                College college = collegeService.queryByName(teaSubject);
                int collegeId = college.getcollegeId();
                List<Major> lists = majorService.queryByCollegerId(collegeId);
                Set<String> set = new HashSet<>();
                for (Major list : lists) {
                    set.add(list.getMajorname());
                }
                if (set.contains(subject)) {
                    flag = true;
                }
            } else {
                return null;
            }
        } else {
            return null;
        }
        if (flag == true) {
            fillbacks.setSubject(fillbacksVal.getSubject());
        } else {
            return null;
        }
        return this.fillbacksService.update(fillbacks);
    }

    /**
     * 根据传入专业查询
     *
     * @param subject
     * @return
     */
    @RequestMapping("/selectSubject")
    public List<Fillbacks> selectSub(String subject) {
        List<Fillbacks> list = fillbacksService.queryBySubject(subject);
        return list;
    }

    /**
     * 模糊查询
     *
     * @param chquestion
     * @return
     */
    @RequestMapping("/likeFill")
    public List<Fillbacks> selectlike(String chquestion) {
        List<Fillbacks> list = fillbacksService.queryByChLike(chquestion);
        return list;
    }

    /**
     * 老师端显示选择题
     *
     * @param session
     * @return
     */
    @RequestMapping("/showFi")
    public List<Fillbacks> showTeaFi(HttpSession session) {
        Integer questionId = (Integer) session.getAttribute("questionId");
        System.out.println(questionId);
        System.out.println("===============");
        String paperquestion = paperService.queryById(questionId).getPaperquestion();
        String[] split = paperquestion.split(",");
        List<Fillbacks> list = new LinkedList<>();
        for (String s : split) {
            int temp = Integer.parseInt(s);
            if (temp > 1000 && temp < 2000) {
                list.add(fillbacksService.queryById(temp));
            }
        }
        return list;
    }

    /**
     * 老师端根据试题显示填空题
     *
     * @param
     * @return
     */
    @RequestMapping("/showTeaFi")
    public List<Fillbacks> showTeaFi(Integer questionId) {
        Integer paperid = questionService.queryById(questionId).getPaperid();
        Paper paper = paperService.queryById(paperid);
        String question = paper.getPaperquestion();
        String[] split = question.split(",");
        List<Fillbacks> res = new LinkedList<>();
        for (String s : split) {
            int temp = Integer.parseInt(s);
            if (temp > 1000 && temp < 2000) {
                res.add(fillbacksService.queryById(temp));
            }
        }
        return res;
    }

    @RequestMapping("/showFill")
    public List<Fillbacks> showFi(Integer stuId, Integer questionId) {
        //System.out.println(questionId);
        Integer paperid =  questionService.queryById(questionId).getPaperid();
        Paper paper = paperService.queryById(paperid);
        String question = paper.getPaperquestion();
        String[] split = question.split(",");
        List<Fillbacks> res = new LinkedList<>();
        for (String s : split) {
            int temp = Integer.parseInt(s);
            if (temp > 1000 && temp < 2000) {
                res.add(fillbacksService.queryById(temp));
            }
        }
        return res;
    }

    @RequestMapping("/correctFill")
    public int correctCh(int[] num, HttpSession session) {
        int ans = 0;
        Integer res = (Integer) session.getAttribute("res");
        if (res == null) {
            for (int i : num) {
                if (i == 1) {
                    ans += 5;
                }
            }
            session.setAttribute("session", ans);
        } else {
            for (int i : num) {
                res += i;
            }
            ans = res;
            session.setAttribute("res", res);
        }
        if (ans == 0) {
            return 0;
        } else {
            return 1;
        }
    }

}